package com.yanqu.road.dao.impl.beautyescort;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.beautyescort.EscortBeautyDispatch;
import com.yanqu.road.entity.beautyescort.EscortBeautyEvent;
import com.yanqu.road.entity.beautyescort.EscortCartData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class EscortCartDataDaoImpl extends TempDataObjectDao<EscortCartData> {

    public static String getEscortCartDataSql = "select * from `t_c_escort_cart`";
    public Map<Long, Map<Integer, EscortCartData>> getEscortCartData() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, Map<Integer, EscortCartData>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getEscortCartDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                EscortCartData data =  parse(rs);
                if(!result.containsKey(data.getUserId())){
                    result.put(data.getUserId(), new ConcurrentHashMap<>());
                }
                result.get(data.getUserId()).put(data.getCartId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertEscortCartDataSql = "insert into `t_c_escort_cart`(userId,cartId,serverId,dartId,clientId,dartTypeId,startTime,reduceTime,takeTime,robbedTimes,autoHandleEvent,beautyDispatchList,state," +
            "getReward,getEventReward,event,enemy) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(EscortCartData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCartId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDartId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getClientId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDartTypeId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStartTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getReduceTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTakeTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRobbedTimes()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isAutoHandleEvent()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBeautyDispatchList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getState()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetEventReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getEvent())));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getEnemy(), ",")));
        if (getDb().executeNoneQuery(insertEscortCartDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateEscortCartDataSql = "update `t_c_escort_cart` set dartId=?,clientId=?,dartTypeId=?,startTime=?,reduceTime=?,takeTime=?,robbedTimes=?,autoHandleEvent=?,beautyDispatchList=?,state=?," +
            "getReward=?,getEventReward=?,event=?,serverId=?,enemy=? where userId=? and cartId=? ";

    @Override
    protected boolean updateData(EscortCartData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getDartId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getClientId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDartTypeId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStartTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getReduceTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTakeTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRobbedTimes()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isAutoHandleEvent()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBeautyDispatchList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getState()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetEventReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getEvent())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getEnemy(), ",")));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCartId()));
        if (getDb().executeNoneQuery(updateEscortCartDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected EscortCartData parseData(ResultSet rs) throws SQLException {
        EscortCartData data = new EscortCartData();
        data.setUserId(rs.getLong("userId"));
        data.setCartId(rs.getInt("cartId"));
        data.setServerId(rs.getLong("serverId"));
        data.setDartId(rs.getLong("dartId"));
        data.setClientId(rs.getInt("clientId"));
        data.setDartTypeId(rs.getInt("dartTypeId"));
        data.setStartTime(rs.getInt("startTime"));
        data.setReduceTime(rs.getInt("reduceTime"));
        data.setTakeTime(rs.getInt("takeTime"));
        data.setRobbedTimes(rs.getInt("robbedTimes"));
        data.setAutoHandleEvent(rs.getBoolean("autoHandleEvent"));
        data.setBeautyDispatchList(JSON.parseObject(rs.getString("beautyDispatchList"), new TypeReference<List<EscortBeautyDispatch>>() {}));
        data.setState(rs.getInt("state"));
        data.setGetReward(rs.getBoolean("getReward"));
        data.setGetEventReward(rs.getBoolean("getEventReward"));
        data.setEvent(JSON.parseObject(rs.getString("event"), EscortBeautyEvent.class));
        if(rs.getString("enemy") != null) {
            data.setEnemy(StringUtils.stringToLongList(rs.getString("enemy"), ","));
        }
        return data;
    }
}
